package dt.day04.demo;

import com.sun.deploy.util.StringUtils;

import java.util.*;

/**
 * Map 作业处理
 * 1：首先整理数据,把数据按照规则整理成行
 */
public class Main {


    public static List<PersonData> personDataList = new LinkedList<>();

    /**
     * 第一题：程序运行前，预加载需要使用的数据
     */
    static {

        List<String []> dataList = new ArrayList<>();

        String [] xiaoMing5Args =  "小明#2021-05#武汉,三亚,广州,景德镇".split("#");
        String [] xiaoMing6Args =  "小明#2021-06#常州,长沙,济南,武汉".split("#");

        String [] xiaoHong5Args =  "小红#2021-05#武汉,株洲,长沙".split("#");
        String [] xiaoHong6Args =  "小红#2021-06#北京,广州,上海".split("#");

        String [] xiaoTong5Args =  "小童#2021-05#济南,常州,巢湖".split("#");
        String [] xiaoTong6Args =  "小童#2021-06#岳阳,济南".split("#");

        dataList.add(xiaoMing5Args);
        dataList.add(xiaoMing6Args);
        dataList.add(xiaoHong5Args);
        dataList.add(xiaoHong6Args);
        dataList.add(xiaoTong5Args);
        dataList.add(xiaoTong6Args);

        //统一处理数据
        for (String [] args : dataList){
            String name = args[0];
            String month = args[1];
            String [] cityArgs = args[2].split(",");
            for (String city : cityArgs){
                PersonData personData = new PersonData(name,month,city);
                personDataList.add(personData);
            }
        }

        //数据整理完毕
        System.out.println("============数据加载【"+personDataList.size()+"】完毕============");
    }


    public static void main(String[] args) {
        //第二题
        getEveryOneToCity();
        System.out.println("============题目分割线============");

        //第三题
        getCityNum();
        System.out.println("============题目分割线============");

        //第四题
        getMonthCityNum();
        System.out.println("============题目分割线============");

        //第五题
        getNameToCityNum();

    }


    /**
     * 第二题：获取每个人去过的城市
     */
    public static void getEveryOneToCity(){

        Map<String,List<String>> personMap = new HashMap<>();
        //遍历数据
        for (PersonData personData : personDataList){
            String key = personData.getName();
            List<String> cityList = personMap.get(key);
            //第一次Map 可能为null，防止空指针
            if (cityList == null){
                cityList = new ArrayList<>();
            }
            //判断城市是否存在
            boolean cityFlag = cityList.contains(personData.getCity());
            if (!cityFlag){
                //如果不存在就添加集合
                cityList.add(personData.getCity());
            }
            //更新数据
            personMap.put(key,cityList);
        }

        //数据处理完，打印数据
        for(Map.Entry<String, List<String>> entry:personMap.entrySet()){
            String name = entry.getKey();
            int size = entry.getValue().size();
            String cityStr = StringUtils.join(entry.getValue(),",");
            String printlnStr = String.format("【%s】去过的城市有【%s】个,分别是:【%s】",name,size,cityStr);
            System.out.println(printlnStr);
        }
    }



    /**
     * 第三题：获取每个城市出现的次数,打印最高和最低的
     */
    public static void getCityNum(){

        Map<String,Integer> cityMap = new HashMap<>();
        int maxNum = 0;
        int minNum = Integer.MAX_VALUE;

        List<String> hotCityList = new ArrayList<>();
        List<String> lowCityList = new ArrayList<>();

        for (PersonData personData : personDataList){
            String key = personData.getCity();
            Integer num = cityMap.get(key);
            if (num == null){
                num = 0;
            }
            //相同的城市+1
            num = num + 1;
            //记录最高，最低的次数
            if (num > maxNum){
                maxNum = num;
            }
            if (num < minNum){
                minNum = num;
            }
            //更新数据
            cityMap.put(key,num);
        }

        //数据处理完，打印数据
        for(Map.Entry<String, Integer> entry:cityMap.entrySet()){
            String name = entry.getKey();
            int size = entry.getValue();
            String printlnStr = String.format("【%s】城市出现了【%s】次",name,size);
            System.out.println(printlnStr);
            if (size == maxNum){
                hotCityList.add(name);
            }else if (size == minNum){
                lowCityList.add(name);
            }
        }

        //打印最热门的城市 和 最冷门的城市
        String hotStr = String.format("最热门的城市为【%s】,出现了【%s】次",StringUtils.join(hotCityList,","),maxNum);
        String lowStr = String.format("最冷门的城市为【%s】,出现了【%s】次",StringUtils.join(lowCityList,","),minNum);
        System.out.println(hotStr);
        System.out.println(lowStr);

    }



    /**
     * 第四题：统计每个月每个城市出现的次数
     */
    public static void getMonthCityNum(){

        Map<String,Integer> cityMap = new HashMap<>();

        for (PersonData personData : personDataList){
            String key = personData.getMonth()+"#"+personData.getCity();
            Integer num = cityMap.get(key);
            if (num == null){
                num = 0;
            }
            num = num + 1;
            //更新数据
            cityMap.put(key,num);
        }

        //数据处理完，打印数据
        for(Map.Entry<String, Integer> entry:cityMap.entrySet()){
            String month = entry.getKey().split("#")[0];
            String city = entry.getKey().split("#")[1];
            int size = entry.getValue();
            String printlnStr = String.format("【%s】-【%s】城市出现了【%s】次",month,city,size);
            System.out.println(printlnStr);
        }

    }



    /**
     * 第五题：统计每个人去的城市的总次数
     */
    public static void getNameToCityNum(){

        Map<String,Integer> cityMap = new HashMap<>();

        for (PersonData personData : personDataList){
            String key = personData.getName();
            Integer num = cityMap.get(key);
            if (num == null){
                num = 0;
            }
            num = num + 1;
            //更新数据
            cityMap.put(key,num);
        }

        // map转换成list进行排序
        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String,Integer>>(cityMap.entrySet());

        // 排序
        Collections.sort(list,new ValueComparator());

        //数据处理完，打印数据
        for(Map.Entry<String, Integer> entry:list){
            String name = entry.getKey();
            int size = entry.getValue();
            String printlnStr = String.format("【%s】-【%s】次",name,size);
            System.out.println(printlnStr);
        }

    }







}
